package com.ljfiction.book.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ljfiction.book.Baner.AddBookBean;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.BaseActivity;
import com.ljfiction.book.adapter.ClassifyAddBookAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/24.
 */

public class BookClassifydialog {
    private Listener listener;
    private Context context;
    private List<AddBookBean.DataBean.ClassificationListBean> plotLableList = new ArrayList<>();
    private Dialog mDialog;
    private ImageView backimageView;
    private RecyclerView tableRecycle;
    private TextView textViewok;
    private ClassifyAddBookAdapter plotLableAdapter;

    public BookClassifydialog(BaseActivity context, List<AddBookBean.DataBean.ClassificationListBean> plotLableList, Listener listener) {
        this.context = context;
        this.listener = listener;
        this.plotLableList = plotLableList;
        mDialog = new Dialog(context, R.style.AppTheme3);
        mDialog.setContentView(R.layout.bookclassify_dialog);
        mDialog.getWindow().setGravity(Gravity.TOP);
        mDialog.getWindow().setWindowAnimations(R.style.ActionSheetDialogAnimation);
        mDialog.setCanceledOnTouchOutside(true);
        backimageView = (ImageView) mDialog.findViewById(R.id.back_bookclassify);
        textViewok = (TextView) mDialog.findViewById(R.id.ok_bookclassify);
        tableRecycle = (RecyclerView) mDialog.findViewById(R.id.gridView_bookclassify);
        plotLableAdapter = new ClassifyAddBookAdapter(R.layout.classfy_item, plotLableList);
        tableRecycle.setAdapter(plotLableAdapter);
        tableRecycle.setLayoutManager(new GridLayoutManager(context, 3));
        mDialog.show();
        intData();
    }

    private void intData() {
        plotLableAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (AddBookBean.DataBean.ClassificationListBean i:
                     plotLableList) {
                    i.setCleck(false);
                }
                plotLableList.get(position).setCleck(true);
                plotLableAdapter.notifyDataSetChanged();
            }
        });
        backimageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });
        textViewok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    for (AddBookBean.DataBean.ClassificationListBean i:
                            plotLableList) {
                        if (i.isCleck()){
                            listener.ok(i.getClassificationname(),String.valueOf(i.getId()));
                            close();
                            return;
                        }
                    }
                }
            }
        });
    }

    public Dialog show() {
        if (mDialog != null) {
            mDialog.show();
        }
        return mDialog;
    }

    public void close() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public static interface Listener {
        public void ok(String str, String id);
    }
}
